//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Discussions.Components;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Forums
{
	/// <summary>
	/// Summary description for IFrameHost.
	/// </summary>
	public class ForumsIFrameHost : BaseForumPage
	{

		#region Child Controls

		protected SectionEditControl SectionEdit1;
		protected GroupEditControl GroupEdit1;
		protected StatusMessage Status;
		protected ResourceLinkButton SaveButton;
		protected ResourceLinkButton DeleteButton;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.DeleteButton.Click += new EventHandler(DeleteButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		

		private void Page_Load(object sender, EventArgs e)
		{
			Status.Visible = false;

			CSContext cntx = CSContext.Current;

			// ID Notes:  -1 = add, 0 = do not display, >0 = edit
			int groupID = cntx.GetIntFromQueryString("GroupID", 0);
			int sectionID = cntx.GetIntFromQueryString("SectionID", 0);
			int parentID = cntx.GetIntFromQueryString("ParentSectionID", -1);

			// Determine whether to display the SectionEdit control or GroupEdit control
			if (groupID > 0 && sectionID != 0)
			{
				SectionEdit1.SectionID = sectionID;
				SectionEdit1.ParentSectionID = parentID;
				SectionEdit1.GroupID = groupID;
				SectionEdit1.Visible = true;
				SaveButton.Visible = true;

				if(sectionID != -1)
					SectionEdit1.Section = Discussions.Components.Forums.GetForum(sectionID, false, true);
				else
					SectionEdit1.Section = new Forum();

				DeleteButton.Visible = SectionEdit1.AllowDelete;

			}
			else if (groupID > 0)
			{
				GroupEdit1.GroupID = groupID;
				GroupEdit1.Visible = true;
				SaveButton.Visible = true;
				DeleteButton.Visible = GroupEdit1.AllowDelete;
			}
			else
			{
				GroupEdit1.Visible = false;
				SectionEdit1.Visible = false;
				DeleteButton.Visible = false;
				SaveButton.Visible = false;
			}
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if( !CSContext.Current.User.IsForumAdministrator ) 
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			bool redirect = false;
			string script = @"
					<script language=""javascript"">
						{0};
					</script>";

			if(!Page.IsValid)
				return;

			if(SectionEdit1.Visible)
			{
				redirect = SectionEdit1.Save();
				string command = string.Format("window.top.updateSectionNode('{0}', '{1}', '{2}', '{3}')", SectionEdit1.GroupID, SectionEdit1.SectionID, SectionEdit1.Section.Name, SectionEdit1.Section.IsActive); 
				script = string.Format(script, command); 
			}

			if(GroupEdit1.Visible)
			{
				redirect = GroupEdit1.Save();
				string command = string.Format("window.top.updateGroupNode('{0}', '{1}')", GroupEdit1.GroupID, GroupEdit1.CurrentGroupName); 
				script = string.Format(script, command); 
			}

			if(redirect)
				Page.RegisterStartupScript("Redirect",script) ;

		}

		private void DeleteButton_Click( Object sender, EventArgs e ) 
		{

			string script = @"
					<script language=""javascript"">
						{0};
						window.frames[""sectionEditFrame""].location.href = null;
					</script>";

			if(SectionEdit1.Visible)
			{
				//SectionEdit1.Delete();
				string command = string.Format("window.top.DeleteSelected('{0}', '{1}')", SectionEdit1.GroupID, SectionEdit1.SectionID);
				script = string.Format(script, command); 
			}

			if(GroupEdit1.Visible)
			{
				//GroupEdit1.Delete();
				string command = string.Format("window.top.DeleteSelected('{0}', '{1}')", GroupEdit1.GroupID, -1);
				script = string.Format(script, command); 
			}

			Page.RegisterStartupScript("Redirect", script);

		}

	}
}
